package com.poisonh.unifiedauth.utils;


import android.util.Log;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TOTPUtils
{
    public static final String MAD_PASS = "abcdefghijklmnop";

    /**
     * 方法描述：验证算法
     *
     * @param token
     * @return
     * @creator chenkexi
     */
    public static String generate(String token)
    {

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String oldDateStr = format.format(new Date()); // 当前时间
        String newMinuteBeginStr = oldDateStr.substring(0, oldDateStr.length() - 2);// 除分钟数
        // 当前时间
        String ordMinuteLastTneStr = oldDateStr.substring(oldDateStr.length() - 2, oldDateStr.length());// 获取当前分钟数
        String newMinuteLastStr = String.valueOf(Integer.valueOf(ordMinuteLastTneStr) - Integer.valueOf(ordMinuteLastTneStr) % 3);// 分钟数-分钟数%3
        String newDateStr = newMinuteBeginStr + newMinuteLastStr;// 最后计算时间

        Log.d("AA", "算法日期:" + newDateStr);
        Log.d("AA", "当前日期:" + new Date());
        Log.d("AA", "计算串:" + token + newDateStr + "abcdefghijklmnop");
        String encryptResult = null;
        try
        {
            encryptResult = Utils.encryptBASE64(encrypt(token + newDateStr + MAD_PASS));

        } catch (Exception e)
        {
            e.printStackTrace();
        }

        System.out.println(encryptResult);
        Log.d("AA", "计算出之后的串:" + encryptResult);
        return encryptResult;
    }

    /**
     * 方法描述：MD5
     *
     * @param str
     * @return
     * @creator chenkexi
     */
    public static byte[] encrypt(String str)
    {
        byte[] bytes = null;
        try
        {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            bytes = md5.digest(str.getBytes());

        } catch (NoSuchAlgorithmException e)
        {
            return bytes;
        }
        return bytes;
    }

}

